package ru.cybertek.struts2.action;

import ru.cybertek.domain.Bank;
import ru.cybertek.dao.BankDao;

import com.opensymphony.xwork2.ActionSupport;

public class BankManagementAction extends ActionSupport 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String fullName;
	
	private String shortName;
	
	private String bic;
	
	private String currentAccount;
	
	private String id;
	
	private BankDao bankDao;
	
	/**
	 * render page with list of banks
	 * 
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String list() throws Exception
	{
		return SUCCESS;
	}
	
	/**
	 * add new bank
	 * 
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception
	{
		Bank bank = new Bank();
		bank.setBic(bic);
		bank.setCurrentAccount(currentAccount);
		bank.setFullName(fullName);
		bank.setShortName(shortName);
		
		if ((id != null) & !id.equals(""))
			bank.setId(Integer.valueOf(id));
		
		bankDao.saveBank(bank);
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	/**
	 * Edit bank value. Get Bank data by Id. 
	 * 
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String edit() throws Exception
	{
		Bank bank = bankDao.getBank(Integer.valueOf(id));
		
		id = bank.getId().toString();
		fullName = bank.getFullName();
		shortName = bank.getShortName();
		bic = bank.getBic();
		currentAccount = bank.getCurrentAccount();
		
		return SUCCESS;
	}
	
	/**
	 * Delete bank
	 * 
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String delete() throws Exception
	{
		bankDao.deleteBank(Integer.valueOf(id));
		
		clearFieldValue();
		
		return SUCCESS;
	}
	
	public String cancel() throws Exception
	{
		clearFieldValue();
		
		return SUCCESS;
	}
	
	/**
	 * clears all fields
	 */
	private void clearFieldValue()
	{
		bic = null;
		currentAccount = null;
		fullName = null;
		shortName = null;
		id = null;
	}

	public String getFullName() 
	{
		return fullName;
	}

	public void setFullName(String fullName) 
	{
		this.fullName = fullName;
	}

	public String getShortName() 
	{
		return shortName;
	}

	public void setShortName(String shortName) 
	{
		this.shortName = shortName;
	}

	public String getBic() 
	{
		return bic;
	}

	public void setBic(String bic) 
	{
		this.bic = bic;
	}

	public String getCurrentAccount() 
	{
		return currentAccount;
	}

	public void setCurrentAccount(String currentAccount) 
	{
		this.currentAccount = currentAccount;
	}

	public String getId() 
	{
		return id;
	}

	public void setId(String id) 
	{
		this.id = id;
	}
	
	public BankDao getBankDao()
	{
		return bankDao;
	}
	
	public void setBankDao(BankDao bankDao)
	{
		this.bankDao = bankDao;
	}
	
}
